package com.ruoyi.quartz.thread;

import com.ruoyi.bizsys.domain.CreditAccount;
import com.ruoyi.business.service.IBLoanInfoService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class BatchJobThread implements Callable {

	private String threadName;

	private Date batchDate;

	//private int startLimit = 0;

	//private int stepLimit = 0;

	private String redisName;
	private RedisCache redisCache;

	private IBLoanInfoService loanInfoService;

	public BatchJobThread(String threadName, Date batchDate, String redisName,
						  IBLoanInfoService loanInfoService,RedisCache redisCache) {
		super();
		this.threadName = threadName;
		this.batchDate = batchDate;
		this.redisName = redisName;
		this.redisCache = redisCache;
		//this.startLimit = startLimit;
		//this.stepLimit = stepLimit;
		this.loanInfoService = loanInfoService;
	}

	@Override
	public String toString() {
		return "BatchJobThread [threadName=" + threadName + ", batchDate=" + DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, this.batchDate)
				+ ", redisName=" + redisName + "]";
	}

	@Override
	public Object call() throws Exception {
		String result = ":success";
		log.info(this.threadName+"=======[START]===" + this.toString());

//		List<CreditAccount> accountList = loanInfoService.getBatchJobOnLoanAccount(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, this.batchDate),
//				this.startLimit, this.stepLimit);
		List<CreditAccount> accountList = redisCache.getCacheList(this.redisName);
		log.info("本次有 {} 个客户需要执行", accountList.size());

		for (CreditAccount account : accountList) {
			log.info("客户号 {} 开始执行跑批操作", account.getCustNo());

			//执行日切任务
			loanInfoService.doBatchJobByCust(batchDate, account);

			log.info("客户号{}执行跑批操作正常结束", account.getCustNo());
		}

		redisCache.deleteObject(this.redisName);

		log.info(this.threadName+"=======[END]===run result:" + result);
		return threadName + result;
	}
}